//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Platforms/Base/IApplication.h"

#if defined( PLATFORM_ANDROID ) or defined( ANDROID_EMULATOR )

#include "Platforms/Android/AKeyCodes.h"
#include "Platforms/Android/AOpenGL.h"
#include "Platforms/Android/AFileSystem.h"
#include "Base/Input/Input.h"
#include "Base/Utils/Log/Logger.h"
#include "Base/Parallel/ParallelThreadManager.h"


namespace Engine
{
namespace Platforms
{

	//
	// Android Base Application
	//

	class AndroidApplication : public IApplication
	{
	// variables
	private:
		ThreadMessageQueue2		_messageQueue;
		ParallelThreadManager	_threadManager;
		EventTransmitter		_events;
		Input					_input;
		Logger					_logger;
		AndroidFileSystem		_filesystem;
		CurrentThread			_thread;


	// methods
	public:
		AndroidApplication (IPlatform *platform, EDevice::type deviceType = EDevice::AUTO) :
			IApplication( platform ), _threadManager( ESS() ),
			_events( platform->GetEventSystem(), ESS() ), _filesystem( ESS() ),
			_input( ESS() ), _logger( ESS() )
		{
			_platform->InitDevice( deviceType );
		}

		~AndroidApplication ()
		{
			_GetLogger()->Close();
			_GetLogger()->CloseCrashFile();
		}


	protected:
		Logger *	_GetLogger ()		{ return &_logger; }
		
		
	// IApplication //
	private:
		virtual void Destroy ()	override
		{
			delete this;
		}


	// ParallelThread //
	public:
		bool IsCurrent () const override
		{
			return _thread.IsCurrent();
		}


		void Flush () override
		{
			_messageQueue.Flush();
		}
		

		void Push (const ParallelOp &op) override
		{
			_messageQueue.Push( op );
		}


		RVALUE_REFERENCE_ONLY(
			void Push (ParallelOp &&op) override
			{
				_messageQueue.Push( ToRValueRef( op ) );
			}
		)


		void Quit () override
		{
			//_platform->Terminate();
		}


		uint ProcessMessages () override
		{
			return _messageQueue.ProcessAll();
		}
	};


}	// Platforms


namespace Base
{
	typedef Platforms::IPlatform			IPlatform;
	typedef Platforms::AndroidApplication	Application;
	typedef Platforms::EKey					EKey;
	typedef Platforms::ESensor				ESensor;
	typedef Platforms::HardwareCamera		HardwareCamera;

}	// Base
}	// Engine

#endif	// PLATFORM_ANDROID or ANDROID_EMULATOR